/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MainPackage;

import java.io.File;
import java.io.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author n.kaminski
 */
public class Options {
    public static final boolean DEBUG = false;
    
    
    
    /**
     * Ist kein Windows System? JA(Ist Linux): linux home. NEIN(Ist Windows): appdata verzeichnis
     */
    private static final String HOME_PATH = (System.getenv("APPDATA") == null ? System.getProperty("user.home") : System.getenv("APPDATA"));
    
    /**
     * Standard Path
     */
    public static final String CONFIG_PATH = HOME_PATH+"/.Ernaehrungsplaner/";
    
   
    public static final String USERXMLPATH = CONFIG_PATH+"User.xml";
    public static final String NAHRUNGSMITTELXMLPATH = CONFIG_PATH+"Food.xml";
    
    
    /**
     * Erstellt den Ordner .Ernaehrungsplaner im Home Verzeichnis (Linux) oder in AppData (Windoof).
     * Und erstellt die Nahrungsmittel in eine XML Datei (Food.xml) im Ordner .Ernaehrungsplaner
     */
    public static void initializeAllFilesAndFolders(){        
        FileWriter fw = null;
        try {
            boolean status;
            File f;
            //Erstelle den Konfigurations Ordner, wo dann die Userdaten und Nahrungsmittel gespeichert werden. 
            f = new File(CONFIG_PATH);
            if(!f.exists()){
                status = f.mkdirs();
                if(DEBUG)
                    System.out.println("Create CONFIG_PATH ("+CONFIG_PATH+"): "+(status ? "success" : "failure"));
            }
            //Erstelle die Standart Nahrungsmittel, wenn diese noch nicht vorhanden sind.
            f = new File(NAHRUNGSMITTELXMLPATH);
            if(!f.exists()){                
                fw = new FileWriter(f);
                      
                InputStream stream = MainWindow.class.getResourceAsStream("/ConfigFiles/NahrungsmittelConfig.txt");            
                if(stream!=null) {
                    InputStreamReader reader=new InputStreamReader(stream);
                    try {
                        fw.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
                        fw.write("<NahrungsmittelList>\n");
                        String content = ""; 
                        int x = reader.read();
                        int id = 0;
                        while(x != -1){
                            content += (char)x;
                            if((char)x == '\n'){
                                id++;
                                writeOneNahrungsmittel(id, content, fw);
                                
                                content = "";
                            }
                            x = reader.read();
                        }
                        fw.write("\t<MaxNahrungsmittelID>"+id+"</MaxNahrungsmittelID>\n");
                        fw.write("</NahrungsmittelList>\n");
                    }
                    catch(Exception ioe) {
                        System.out.println("Da lauft was falsch");
                    }                
                    
                    
                }
                else {
                    /* was auch immer */
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Options.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if(fw != null )
                    fw.close();
            } catch (IOException ex) {
                Logger.getLogger(Options.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    private static void writeOneNahrungsmittel(int id, String con, FileWriter fw){
        try {
            con = con.replace("\n", "");
            
            String[] f1 = con.split("\",");
            f1[1] = f1[1].replace(" ", "");
            String[] f2 = f1[1].split(",");
            fw.write("\t<Nahrungsmittel>\n");
                fw.write("\t\t<broteinheiten>"+f2[2]+"</broteinheiten>\n");     // 1
                fw.write("\t\t<calcium_mg>"+f2[10]+"</calcium_mg>\n");          //10
                fw.write("\t\t<carotin_mg>"+f2[15]+"</carotin_mg>\n");          //21
                fw.write("\t\t<cholesterin_mg>"+f2[4]+"</cholesterin_mg>\n");   // 4
                fw.write("\t\t<eisen_mg>"+f2[12]+"</eisen_mg>\n");              //12
                fw.write("\t\t<eiweiss_g>"+f2[3]+"</eiweiss_g>\n");             // 3
                fw.write("\t\t<fett_g>"+f2[5]+"</fett_g>\n");                   // 5
                fw.write("\t\t<folsaeure_mueg>"+f2[17]+"</folsaeure_mueg>\n");  //20
                fw.write("\t\t<kalium_mg>"+f2[8]+"</kalium_mg>\n");             // 8
                fw.write("\t\t<kcal>"+f2[0]+"</kcal>\n");                       // 0
                fw.write("\t\t<kohlenhydrate>"+f2[1]+"</kohlenhydrate>\n");     // 2
                fw.write("\t\t<magnesium_mg>"+f2[9]+"</magnesium_mg>\n");       // 9
                fw.write("\t\t<nahrungsmittelID>"+id+"</nahrungsmittelID>\n");  //id
                fw.write("\t\t<name>"+f1[0].subSequence(1, f1[0].length()) +"</name>\n");//name
                fw.write("\t\t<natrium_mg>"+f2[7]+"</natrium_mg>\n");           // 7
                fw.write("\t\t<phosphor_mg>"+f2[11]+"</phosphor_mg>\n");        //11
                fw.write("\t\t<ungesFS_g>"+f2[6]+"</ungesFS_g>\n");             // 6
                fw.write("\t\t<vitamin_A_mueg>"+f2[14]+"</vitamin_A_mueg>\n");  //14
                fw.write("\t\t<vitamin_B1_mg>"+f2[18]+"</vitamin_B1_mg>\n");    //15
                fw.write("\t\t<vitamin_B2_mg>"+f2[19]+"</vitamin_B2_mg>\n");    //16
                fw.write("\t\t<vitamin_B6_mg>"+f2[20]+"</vitamin_B6_mg>\n");    //17
                fw.write("\t\t<vitamin_C_mg>"+f2[21]+"</vitamin_C_mg>\n");      //18
                fw.write("\t\t<vitamin_E_mg>"+f2[16]+"</vitamin_E_mg>\n");      //19
                fw.write("\t\t<zink_mg>"+f2[13]+"</zink_mg>\n");                //13
            fw.write("\t</Nahrungsmittel>\n");
        } catch (IOException ex) {
            Logger.getLogger(Options.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("FEHLER");
        }
    }
   
    public static Calendar createGregorianCalendar(int jahr, int monat, int tag){
        Date d = new Date();
        Calendar c = new GregorianCalendar(2013, Calendar.JANUARY, 7);
        //c.setTime(d);
        return c;
    }
}
